package com.beta.resource.utils;

import java.io.IOException;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.ExecuteWatchdog;

import com.beta.resource.utils.shell.BaseShellProcessDestroyer;
import com.beta.resource.utils.shell.BaseShellStreamHandler;

public class BaseShell {
    public static final long EXEC_TIMEOUT = 1 * 60;

    public static void exec(String cmd, onStatusChange listener, Object data) throws ExecuteException, IOException {
        DefaultExecutor executor = new DefaultExecutor();
        BaseShellStreamHandler streamHandler = new BaseShellStreamHandler(listener, data);
        BaseShellProcessDestroyer destoryer = new BaseShellProcessDestroyer();
        streamHandler.setProcessDestoryer(destoryer);
        executor.setStreamHandler(streamHandler);
        executor.setProcessDestroyer(destoryer);
        executor.setWatchdog(new ExecuteWatchdog(EXEC_TIMEOUT));
        // BaseShellResultHandler resultHandler = new BaseShellResultHandler();
        executor.execute(CommandLine.parse(cmd));
    }

    public static void exec(String cmd) throws ExecuteException, IOException{
        DefaultExecutor executor = new DefaultExecutor();
        executor.setWatchdog(new ExecuteWatchdog(EXEC_TIMEOUT));
        executor.execute(CommandLine.parse(cmd));
    }
}
